package com.example.demo1.action;

import com.example.demo1.model.Flight;
import com.example.demo1.model.Order;
import com.example.demo1.service.FlightService;
import com.example.demo1.service.OrderService;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderAction {

    @Autowired
    OrderService orderService;
    @Autowired
    FlightService flightService;

    //陈泽源10.17
    @RequestMapping("/getOrderlist")
    public String getOrderList(Model model){

        List<Order> orderList = orderService.getOrderList();
        model.addAttribute("orderList",orderList);
        model.addAttribute("active", 3);
        return "/ManageOrder";
    }

    //陈泽源10.17
    @RequestMapping("/getOrders")
    public String getOrders(String orderId,Model model){
        System.out.println("orderId"+orderId);
        if(orderId==null)
        {
            List<Order> orderList = orderService.getOrderList();
            model.addAttribute("orderList",orderList);
        }
        else
        {
            try{
                int orderid = Integer.parseInt(orderId);
                List<Order> orderList = orderService.getOrders(orderid);
                model.addAttribute("orderList",orderList);
            }
            catch (Exception e)
            {
                List<Order> orderList = orderService.getOrderList();
                model.addAttribute("orderList",orderList);
            }
        }
        model.addAttribute("active", 3);
        return "/ManageOrder";
    }

    //陈泽源10.18
    @RequestMapping("/deleteOrder")
    public String deleteOrder(int orderId, HttpServletRequest httpServletRequest,Model model){
        List<Order> orders = orderService.getOrders(orderId);
        System.out.println("userId:"+orders.get(0).getUserId());
        int result = orderService.deleteOrder(orderId);
        System.out.println("deleteOrderId:"+orderId+" "+result);

        if(result==1)
        {
            System.out.println("dOrderId:"+orderId+" "+result);
            httpServletRequest.setAttribute("orderId",orders.get(0).getOrderId());
            httpServletRequest.setAttribute("userId",orders.get(0).getUserId());
            return "forward:/message/addMessage";
        }
        model.addAttribute("active", 3);
        model.addAttribute("result","no");
        model.addAttribute("msg","删除失败");
        return "/result";
    }

    //陈泽源10.19
    @RequestMapping("/updateOrder")
    public String updateOrder(int orderId,Model model){
        List<Order> orders = orderService.getOrders(orderId);
        System.out.println("userId:"+orders.get(0).getUserId());
        Flight flight = flightService.getAFlight(orders.get(0).getFlightId());
        model.addAttribute("active", 3);
        model.addAttribute("order",orders.get(0));
        model.addAttribute("flight",flight);
        return "/updateOrder";
    }
}
